<template>
	<view class="page-container">
		<view class="header-view-box">
			<cu-custom bgColor="bg-blue" :isBack="true">
						<block slot="backText"></block>
						<block slot="content">
							<text class="nav-title">特惠折扣车</text>
						</block>
					</cu-custom>
			<addressSearchFixed ref="setAddres" style="position: initial;" @searchBtn='searchBtn' :isShowAddress="false"></addressSearchFixed>
		
			<view>
				<ms-dropdown-menu style="position: absolute;width: 100%;"  :style="[{top:dropdownTop + 'px'}]">
					<view @click="handleSelectCar">
						<ms-dropdown-item   v-model="carModel" :isLinke="true" :hasSlot="false" title="所有车型" ref="dropdownItemModel">
							
						</ms-dropdown-item>
					</view>
					<ms-dropdown-item v-model="carColor" :hasSlot="true" :isLinke="false" :title="carColor.name" ref="dropdownItemColor">
						<view class="dropdown-item-content">
							<view v-for="item in colorList" class="row-dropdown" @click="handleClickColor(item)">
								<view :class="item.name==carColor.name?'row-dropdown-select':''" class="row-dropdown-text">{{item.name}}</view>
								<view class="row-dropdown-num">{{item.num}}条</view>
							</view>
						</view>
					</ms-dropdown-item>
					<ms-dropdown-item v-model="carType" :hasSlot="true" :isLinke="false" :title="carType.name" ref="dropdownItemType">
						<view class="dropdown-item-content">
							<view v-for="item in TypeList" class="row-dropdown" @click="handleClickType(item)">
								<view :class="item.id==carType.id?'row-dropdown-select':''" class="row-dropdown-text">{{item.name}}</view>
								<view class="row-dropdown-num">{{item.num}}条</view>
							</view>
						</view>
					</ms-dropdown-item>
					<ms-dropdown-item v-model="carRegion" :hasSlot="true" :isLinke="false" :title="carRegion.name" ref="dropdownItemRegion">
						<view class="dropdown-item-content">
							<view v-for="item in regionList" class="row-dropdown" @click="handleClickRegion(item)">
								<view :class="item.id==carRegion.id?'row-dropdown-select':''" class="row-dropdown-text">{{item.name}}</view>
								<view class="row-dropdown-num">{{item.num}}条</view>
							</view>
						</view>
					</ms-dropdown-item>
				</ms-dropdown-menu>
			</view>
			<view id="id_dingwei"></view>
		</view>
		<view :style="{'height':headerHeight + 'px'}" v-if="isShowList"></view>
		<view v-if="isShowList">
			<scroll-view scroll-y="true" :style="'height:' + scroll_height+';top:'+scrollViewTop + 'px'" style="position: fixed;width: 100%;padding-bottom: 100px;" :scroll-top="scrollTop" refresher-enabled
				:refresher-triggered="triggered" @refresherrefresh="onRefresh" @scrolltolower="lower">
				<view class="contentViewBox">
					<view class="dis_car_card"  v-for="(car,index) in waterfall.list" @click="detailCar(car)">
						<view class="image"><image :src="car.url" mode="widthFix"></image></view>
						<view class="dis_car_info">
							<view class="dis_car_info_title">{{car.cars_name}}</view>
							<view class="dis_car_info_sub">{{car.cars_type}} {{car.appearance_interior}}</view>
							<view class="dis_car_info_description" style="display: flex;justify-content: space-between;"><span v-if="car.remark">备注:{{car.remark}}</span><span v-else></span> <view class="dis_car_info_bottom_time">{{car.create_time}}</view></view>
							
							<view class="dis_car_info_bottom">
								<view class="dis_car_info_bottom_price">售价:{{car.price}} <text v-if="car.price > 0">万</text> </view>
								<view class="dis_car_info_bottom_guide" v-if="car.guide_price">指导价:{{car.guide_price}}<span>万</span></view>
							</view>
						</view>
					</view>
					<image class="emptyData" v-if="waterfall.list.length<1" src="@/static/empty.png" mode=""></image>
				</view>
				<view v-if="waterfall.status == 'await'">
					<view class="load-txt">上拉加载更多</view>
				</view>
				<view v-else-if="waterfall.status == 'loading'">
					<view class="load-txt">加载中...</view>
				</view>
				<view v-else-if="waterfall.status == 'finish'">
					<view class="load-txt">没有更多了</view>
				</view>
				<view v-else-if="waterfall.status == 'fail'">
					<image class="load-icon" src="@/static/NoDataBac.png"></image>
					<view class="load-txt">出错了，请刷新重试</view>
				</view>
				<view v-else-if="waterfall.status == 'empty'">
					<image class="load-icon" src="@/static/NoDataBac.png"></image>
					<view class="load-txt">暂无数据</view>
				</view>
				<view v-else>
					<view class="load-txt-yincang">没有数据了</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import cuCustom from "@/components/custom-nav/cu-custom.vue"
	import addressSearchFixed from "@/components/addressSearch/addressSearchFixed.vue";
	import msDropdownMenu from '@/components/ms-dropdown/dropdown-menu.vue'
	import msDropdownItem from '@/components/ms-dropdown/dropdown-item.vue'
	import {
		getTypeList,getCityList,getColorList,getCarList
	} from '@/api/discount.js'
	export default {
		components: {
			addressSearchFixed,
			msDropdownMenu,
			msDropdownItem,
			cuCustom
		},
		data() {
			return {
				isfirst: true,
				isRefresher: true,
				//上拉加载下拉刷新相关
				last_page: 0, //总页数
				isShowMore: false,
				isEnd: false,
				triggered: false,
				isfreshing: false,
				isShowList: true,
				isNoData: false,
				scroll_height: '',
				scrollTop: 0,
				// 异步请求相关
				ajax: {
					// 是否可以加载
					load: true,
					// 数据列表
					dataList: []
				},
				// 瀑布流组件相关
				waterfall: {
					status: "",
					reset: false,
					list: []
				},
				isfirst: true,
				isShowList: true,
				headerHeight: '',
				postData: {
					page: 1, //第几页数据
					page_num: 10, // 行数 必传 其他都是非必须
					order: 0, // 排序0：默认排序 1：最新上架 2：价格最低 3：价格最高 4：里程最少 5：车龄最短
					remark_id: '', //品牌id
					color_name: '', //二级车系id
					city_id: '', // 价格起 
					brand_id: '', // 价格止
					series_two_id: '', // //车龄起  
					search: '' //搜索框输入的内容
				},
				triggered:false,
				dropdownTop: this.CustomBar+this.SeachBar,
				scrollViewTop: this.CustomBar+this.SeachBar+46,
				scrollTop:0,
				regionList: [{"name":"全部地区"}],
				colorList: [{"name":"全部颜色"}],
				TypeList: [{"name":"全部类型"}],
				carModel: "",
				carColor: "",
				carType:"",
				carRegion:"",
				value3: 'init',
				scroll_height:"calc(100vh - 132px)",
				carInfo:[{
					title:"北京BJ90 21款 4.0T尊驾",
					sub:"国产现车 墨玉黑丨赤金  运损车",
					description:"备注:本家开票，增值用户都行。开票巴拉巴拉罢了吧啦啦啦啦啦",
					price:"33.80",
					guidePrice:"16.54",
					time:"刚刚"
				},{
					title:"北京BJ90 21款 4.0T尊驾",
					sub:"国产现车 墨玉黑丨赤金  运损车",
					description:"备注:本家开票，增值用户都行。开票巴拉巴拉罢了吧啦啦啦啦啦",
					price:"33.80",
					guidePrice:"16.54",
					time:"刚刚"
				}]
			};
		},
		onHide() {
			try{
				this.$utils.movePage()
			}catch(e){
				//TODO handle the exception
			}
		},
		onUnload() {
			try{
				this.$utils.movePage()
			}catch(e){
				//TODO handle the exception
			}
		},
		mounted() {
			this.getheaderHeight()
		},
		onLoad() {
			this.carType=this.TypeList[0]
			this.getColorList();
			this.getTypeList();
			this.getCityList();
			this.getCarList();
		},
		onReady() {
			this.getheaderHeight()
			const query = uni.createSelectorQuery().in(this);
			query.select('#id_dingwei').boundingClientRect(data => {
				this.$utils.log("得到布局位置信息" + JSON.stringify(data));
				var that = this
				that.isShowList = false
				that.$nextTick(() => {
					that.scroll_height = uni.getSystemInfoSync().windowHeight - data.top + 'px';
					that.top_height = data.top + 20 + 'px'
					that.isShowList = true
					this.$utils.log("topgaodu   " + that.top_height);
					this.$utils.log("that.scroll_height  " + that.scroll_height);
				})
		
			}).exec();
		},
		methods: {
			// 跳转详情页面
			detailCar: function (data) {
				console.log('详情===', data);
				uni.navigateTo({
					url: "/pages/home/newCar/newCarDetail/newCarDetail?id=" + data.id
				})
			},
			//下拉刷新
			onRefresh() {
				// 正常情况下接口返回应该很会很快。故意延迟调用，让用户有在刷新的体验感
				setTimeout(() => {
					this.postData.page = 1;
					this.ajax.load = true;
					this.isRefresher = true;
					this.getCarList()
				}, 800);
			},
			// 上拉加载
			lower() {
				this.postData.page = this.postData.page + 1
				if (this.waterfall.status == '' || this.waterfall.status == 'finish' || this.waterfall.status ==
					'loading') {
					uni.showToast({
						title: '没有数据了',
						icon: 'none'
					})
					this.$utils.log("没有数据了")
					return
				}
				this.getCarList()
			},
			searchBtn(content) {
				this.postData.search = content
				this.postData.page = 1;
				this.ajax.load = true;
				this.isRefresher = false;
				this.getCarList()
			},
			//获取车源列表数据
			getCarList() {
				this.$utils.log("打印请求的参数   " + JSON.stringify(this.postData))
				if (!this.ajax.load) {
					return;
				}
			
				if (!this.isfirst & this.postData.page == 1 & this.isRefresher) {
					this.triggered = true
				}
				
				this.isfirst = false
				this.ajax.load = false;
				// 设置状态为加载中
				this.waterfall.status = 'loading';
				var that = this
				this.$utils.log("打印请求的参数2222" + JSON.stringify(this.postData))
				getCarList(that.postData).then(res => {
						that.$utils.log("打印服务端返回的车源列表数据" + JSON.stringify(res))
						if (res.code == 0) {
							// that.isShowList = false
							that.$nextTick(() => {
								this.$utils.log("打印请求的参数" + JSON.stringify(this.postData))
								// 第一页数据执行以下代码
								if (that.postData.page == 1 && that.triggered) {
									//关闭下拉
									that.triggered = false
								}
								// 第一页没有数据时处理
								if (res.data.data.length < 1 & that.postData.page == 1) {
									// 设置组件为 暂无数据 状态
									that.waterfall.status = 'empty';
									// 因没有数据 直接[]赋值给瀑布流 list 属性
									that.waterfall.list = []
									return;
								}
								// 数据无效时处理
								if (!res.data.data || res.data.data.length < 1) {
									// 设置组件为 加载结束 状态
									that.waterfall.status = 'finish';
									setTimeout(() => {
										that.waterfall.status = ''
									}, 3000)
									return;
								}
								// 将数据赋值给瀑布流 list 属性
								if (that.postData.page == 1) {
									that.waterfall.list = res.data.data;
								} else {
									that.waterfall.list = [...that.waterfall.list, ...res.data.data];
								}
			
								// 设置组件为 加载成功 状态，此时瀑布流组件开始计算当前数据的布局
								if (that.postData.page < res.data.last_page) {
									that.waterfall.status = 'await';
									that.ajax.load = true
								} else {
									that.waterfall.status = 'finish';
									setTimeout(() => {
										that.waterfall.status = ''
									}, 3000)
									return;
								}
			
			
								// 缓存当前数据给其他需要该数据的功能使用
								if (that.postData.page == 1) {
									that.ajax.dataList = res.data.data;
								} else {
									that.ajax.dataList = [...that.ajax.dataList, ...res.data.data];
								}
							})
						} else {
							// 设置组件为 暂无数据 状态
							that.waterfall.status = 'empty';
							// 因没有数据 直接[]赋值给瀑布流 list 属性
							that.waterfall.list = []
						}
			
					},
					error => {
						this.$utils.log("打印进入  error  " + JSON.stringify(error))
						// 设置组件为 暂无数据 状态
						that.waterfall.status = 'empty';
						// 因没有数据 直接[]赋值给瀑布流 list 属性
						that.waterfall.list = []
					})
			},
			//获取高度
			getheaderHeight() {
				uni.createSelectorQuery().in(this).select('.header-view-box').boundingClientRect(data => {
					// this.$utils.log("打印获取固定在顶部的view的高度 =====  " + JSON.stringify(data))
					this.headerHeight = data.height
				}).exec()
			},
			getColorList(){
			    getColorList().then(res => {
					if (res.code == 0) {
					  this.colorList = res.data;
					  this.carColor=this.colorList[0];
					}
				})	
			},
			getTypeList() {
				getTypeList().then(res => {
					if (res.code == 0) {
						this.TypeList = res.data
						this.TypeList[0]['id'] = 0;
						this.carType = this.TypeList[0];
					}
				})
			},
			getCityList(){
				getCityList().then(res => {
					if (res.code == 0) {
						this.regionList = res.data;
						this.regionList[0]['id'] = 0;
						this.carRegion = this.regionList[0]
					}
				})
			},
			backData(object) {
				/*if(object.isRefresh){
					this.postData.page = 1;
					this.ajax.load = true;
					this.isRefresher = false;
					this.getPageSecondhandCarList()
				}*/
				console.log("item3---");
				console.log(JSON.stringify(object));
				console.log("item3---");
				this.postData.page = 1;
				this.ajax.load = true;
				this.isRefresher = false;
				if(object.id){
				   this.postData.series_two_id = object.id;
				}else{
				   this.postData.series_two_id = "";
				}
				
				if(object.brand_id){
				   this.postData.brand_id = object.brand_id;
				}else{
				   this.postData.brand_id = "";
				}
				this.getCarList()
				this.$refs.dropdownItemType.closePopup()
			},
			handleClickRegion(item){
				this.carRegion=item
				this.$refs.dropdownItemRegion.closePopup()
				if(item.name!="全部地区"){
				   this.postData.city_id = item.id;
				}else{
				   this.postData.city_id = "";
				}
				this.postData.page = 1;
				this.ajax.load = true;
				this.isRefresher = false;
				this.getCarList()
			},	
			handleClickColor(item){
				this.carColor=item
				this.$refs.dropdownItemColor.closePopup()
				if(item.name!="全部颜色"){
				   this.postData.color_name = item.name;
				}else{
				   this.postData.color_name = "";
				}
				this.postData.page = 1;
				this.ajax.load = true;
				this.isRefresher = false;
				this.getCarList()
			},
			handleClickType(item){
				this.carType=item;
				if(item.name!="全部类型"){
				   this.postData.remark_id = item.id;
				}else{
				   this.postData.remark_id = "";
				}
				this.postData.page = 1;
				this.ajax.load = true;
				this.isRefresher = false;
				this.getCarList()
				this.$refs.dropdownItemType.closePopup()
			},
			handleSelectCar(){
				this.$refs.dropdownItemModel.closePopup()
				uni.navigateTo({
					url:"/main/discountCar/selectCarModel/selectCarModel"
				})
			}
        }
	}
</script>

<style lang="scss" scoped>
	.emptyData{
		width: 498rpx;
		margin: calc(50% - 200rpx) calc(50% - 249rpx) 0;
	}
	.dis_car_card{
		background-color: #fff;
		display: flex;
		justify-content: space-between;
		margin-bottom:20rpx;
		padding: 30rpx 20rpx;
		background: #FFFFFF;
		border-radius: 14rpx;
		.image{
			width: 174rpx;
			height: 129rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			overflow: hidden;
			image{
				width: 100%;
			}
		}
	}
	.dis_car_info{
		width:calc(100% - 174rpx) ;
		margin-left: 20rpx;
		
		.dis_car_info_title{
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 800;
			color: #000000;
			margin-bottom: 18rpx;
		}
		&_sub{
			font-size: 24rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #333333;
			margin-bottom: 10rpx;
		}
		&_description{
			font-size: 20rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #999999;
			margin-bottom: 20rpx;
			display: -webkit-box !important;
			word-break: break-all;
			text-overflow: ellipsis;
			overflow: hidden;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
		}
		&_bottom{
			display: flex;
			justify-content: space-between;
			&_price{
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: 800;
				color: #EC4B4B;
			}
			&-guide{
				font-size: 20rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #333333;
			}
			&_time{
				font-size: 20rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
			}
		}
	}
.row-dropdown{
	padding: 30rpx ;
	display: flex;
	justify-content: space-between;
	.row-dropdown-num{
		color: #A5A5A5;
	}
	.row-dropdown-text{
		font-weight: bold;
	}
}
.row-dropdown-select{
	color: #3855BE;
}
.contentViewBox{
	background-color: #F7F8FA;
	height: 100%;
	padding: 28rpx;
}
.allModel{
	width: 91px;
}
.load-txt {
	padding: 0 0 20rpx 0;
	text-align: center;
	color: #999;
	font-size: 24rpx;
}

.load-txt-yincang {
	padding: 0 0 20rpx 0;
	text-align: center;
	color: white;
	font-size: 24rpx;
}

.load-icon {
	width: 300rpx;
	height: 300rpx;
	margin: 0 auto 20rpx auto;
	display: block;
}
</style>